package cn.gogpay.duoim.sdk.model;

import cn.gogpay.duoim.sdk.constant.IMRespCode;
import com.alibaba.fastjson.JSON;

import java.io.Serializable;


public class IMRespResult<T> implements Serializable {

    /**
     * 状态
     */
    private Integer errCode;

    /**
     * 信息
     */
    private String errMsg;

    /**
     * 详情
     */
    private String errDlt;

    /**
     * 数据
     */
    private T data;

    public IMRespResult() {
        this(null);
    }

    public IMRespResult(T t) {
        this(IMRespCode.success().getCode(), IMRespCode.success().getMessage(), t);
    }

    public IMRespResult(Integer errCode, String msg, T data) {
        this.errCode = errCode;
        this.errMsg = msg;
        this.data = data;
    }

    public Integer getErrCode() {
        return errCode;
    }

    public void setErrCode(Integer errCode) {
        this.errCode = errCode;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }

    public String getErrDlt() {
        return errDlt;
    }

    public void setErrDlt(String errDlt) {
        this.errDlt = errDlt;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    private static <T> IMRespResult<T> build(Integer status, String msg, T data) {
        return new IMRespResult<>(status, msg, data);
    }

    public static <T> IMRespResult<T> success(Integer status, String msg, T data) {
        return build(status, msg, data);
    }

    public static <T> IMRespResult<T> success(String msg, T data) {
        return success(IMRespCode.success().getCode(), msg, data);
    }

    public static <T> IMRespResult<T> success(IMRespCode respCode, T data) {
        return success(respCode.getCode(), respCode.getMessage(), data);
    }

    public static <T> IMRespResult<T> success(T data) {
        return success(IMRespCode.success(), data);
    }

    public static <T> IMRespResult<T> success() {
        return success(null);
    }

    public static <T> IMRespResult<T> successNull(String msg) {
        return success(IMRespCode.success().getCode(), msg, null);
    }

    public static <T> IMRespResult<T> failure(Integer status, String msg, T data) {
        return build(status, msg, data);
    }

    public static <T> IMRespResult<T> failure(Integer status, String msg) {
        return failure(status, msg, null);
    }

    public static <T> IMRespResult<T> failure(IMRespCode respCode) {
        return failure(respCode.getCode(), respCode.getMessage());
    }

    public static <T> IMRespResult<T> failure() {
        return failure(IMRespCode.error());
    }

    public static <T> IMRespResult<T> failureNull(String msg) {
        return failure(IMRespCode.error().getCode(), msg);
    }

    public static <T> boolean hasSuccess(IMRespResult<T> result) {
        if (result == null) {
            return false;
        }
        return IMRespCode.success().getCode().equals(result.getErrCode());
    }

    public static <T> boolean compare(IMRespResult<T> result, IMRespCode code) {
        if (result == null) {
            return false;
        }
        return code.getCode().equals(result.getErrCode());
    }

    public static <T> boolean hasFailure(IMRespResult<T> result) {
        return !hasSuccess(result);
    }

    public static <T> boolean hasOk(IMRespResult<T> result) {
        if (result == null) {
            return false;
        }
        return IMRespCode.ok().getCode().equals(result.getErrCode());
    }

    public static <T> boolean hasUnOk(IMRespResult<T> result) {
        return !hasOk(result);
    }
}
